const Menu = require('../../../models/admin_menu');

/**
 * 菜单管理控制器
 */
const ControllerMenu = {
    /* 获取列表 */
    getList: async function (req, res, next) {
        try {
            const menus = await Menu.all();

            res.json({ code: 200, data: menus })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 获取详情 */
    getById: async function (req, res, next) {
        try {
            let id = req.params.id;
            const menuItem = await Menu.select({ id });
            res.json({ code: 200, data: menuItem[0] })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 创建 */
    Insert: async function (req, res, next) {
        try {
            let params = {
                pid: req.body.pid ? Number(req.body.pid) : 0,
                name: req.body.name ? String(req.body.name) : '',
                type: req.body.type ? Number(req.body.type) : 1,
                display: req.body.display ? Number(req.body.display) : 1,
                sort: req.body.sort ? Number(req.body.sort) : 0,
                created_time: new Date().getTime()
            }

            switch (params.type) {
                case 1: //* 分类类型添加icon
                    params.icon = req.body.icon;
                    break;
                case 2: //* 菜单类型添加路由URL和权限
                    params.path = req.body.path;
                    params.code = req.body.code;
                    break;
                case 3: //* 按钮类型添加接口和权限
                    params.api = req.body.api;
                    params.code = req.body.code;
                    break;
                case 4: //* 动作类型添加接口和权限
                    params.api = req.body.api;
                    params.code = req.body.code;
                    break;
            }

            if (!params.name) return res.json({ code: 0, msg: "菜单名必须填写" });

            if (!params.type) return res.json({ code: 0, msg: "请选择分类" });

            await Menu.insert(params)

            res.json({ code: 200, msg: '新增成功' })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 编辑 */
    update: async function (req, res, next) {
        try {
            let id = req.body.id;
            let params = {
                pid: req.body.pid ? Number(req.body.pid) : 0,
                name: req.body.name ? String(req.body.name) : '',
                type: req.body.type ? Number(req.body.type) : 1,
                display: req.body.display ? Number(req.body.display) : 0,
                sort: req.body.sort ? Number(req.body.sort) : 0,
                update_time: new Date().getTime()
            }

            switch (params.type) {
                case 1: // 分类类型编辑icon
                    params.icon = req.body.icon;
                    break;
                case 2: // 菜单类型编辑路由URL和权限
                    params.path = req.body.path;
                    params.code = req.body.code;
                    break;
                case 3: // 按钮类型编辑接口和权限
                    params.api = req.body.api;
                    params.code = req.body.code;
                    break;
                case 4: // 动作类型编辑接口和权限
                    params.api = req.body.api;
                    params.code = req.body.code;
                    break;
            }

            if (!params.name) return res.json({ code: 0, msg: "菜单名必须填写" });

            if (!params.type) return res.json({ code: 0, msg: "请选择分类" });

            await Menu.update(id, params);

            res.json({ code: 200, msg: '编辑成功' })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 删除 */
    del: async function (req, res, next) {
        try {
            let id = req.body.id;
            await Menu.delete(id)

            res.json({ code: 200, msg: "成功" })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerMenu;